# import numpy as np
# import pydantic
# from pydantic import BaseModel
# from langchain.embeddings.base import Embeddings
# from typing import List, Any
#
#
# class BaseResponse(BaseModel):
#     code: int = pydantic.Field(200, description="API status code")
#     msg: str = pydantic.Field("success", description="API status message")
#     data: Any = pydantic.Field(None, description="API data")
#
#     class Config:
#         schema_extra = {
#             "example": {
#                 "code": 200,
#                 "msg": "success",
#             }
#         }
#
#
# def normalize(embeddings: List[List[float]]) -> np.ndarray:
#     '''
#     sklearn.preprocessing.normalize 的替代（使用 L2），避免安装 scipy, scikit-learn
#     '''
#     norm = np.linalg.norm(embeddings, axis=1)
#     norm = np.reshape(norm, (norm.shape[0], 1))
#     norm = np.tile(norm, (1, len(embeddings[0])))
#     return np.divide(embeddings, norm)
#
#
# def embed_texts(
#         texts: List[str],
#         embeddings,
#         to_query: bool = False,
# ) -> BaseResponse:
#     '''
#     对文本进行向量化。返回数据格式：BaseResponse(data=List[List[float]])
#     '''
#     return BaseResponse(data=embeddings.embed_documents(texts))
#
#
# async def aembed_texts(
#         texts: List[str],
#         embeddings,
#         to_query: bool = False,
# ) -> BaseResponse:
#     '''
#     对文本进行向量化。返回数据格式：BaseResponse(data=List[List[float]])
#     '''
#     return BaseResponse(data=await embeddings.aembed_documents(texts))
#
#
# class EmbeddingsFunAdapter(Embeddings):
#     def __init__(self, embed_model):
#         self.embed_model = embed_model
#
#     def embed_documents(self, texts: List[str]) -> List[List[float]]:
#         embeddings = embed_texts(texts=texts, embeddings=self.embed_model, to_query=False).data
#         return normalize(embeddings).tolist()
#
#     def embed_query(self, text: str) -> List[float]:
#         embeddings = embed_texts(texts=[text], embeddings=self.embed_model, to_query=True).data
#         query_embed = embeddings[0]
#         query_embed_2d = np.reshape(query_embed, (1, -1))  # 将一维数组转换为二维数组
#         normalized_query_embed = normalize(query_embed_2d)
#         return normalized_query_embed[0].tolist()  # 将结果转换为一维数组并返回
#
#     async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
#         embeddings = (await aembed_texts(texts=texts, embeddings=self.embed_model, to_query=False)).data
#         return normalize(embeddings).tolist()
#
#     async def aembed_query(self, text: str) -> List[float]:
#         embeddings = (await aembed_texts(texts=[text], embeddings=self.embed_model, to_query=True)).data
#         query_embed = embeddings[0]
#         query_embed_2d = np.reshape(query_embed, (1, -1))  # 将一维数组转换为二维数组
#         normalized_query_embed = normalize(query_embed_2d)
#         return normalized_query_embed[0].tolist()  # 将结果转换为一维数组并返回
